package servlet.api;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.MerchantApplication;
import service.MerchantApplicationManager;
import service.factory.ManagerFactory;
import util.JsonUtil;
import dto.ResponseDto;

public class AduitMerchantServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private MerchantApplicationManager manager;
       
    public AduitMerchantServlet() {
        super();
       manager = ManagerFactory.getApplicationManagerInstance();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String str = JsonUtil.getJsonStringfromStream(request.getInputStream());
		System.out.println(str);
		ResponseDto responseDto = new ResponseDto();
		if (str==null||"".equals(str)) {
			responseDto.setCode(-1);
			responseDto.setMessage("message can not be null");
			
		}else{
			MerchantApplication application = (MerchantApplication) JsonUtil.toJsonObject(str, MerchantApplication.class);
			System.out.println(application.toString());
			String result = manager.audit(application);
			if ("success".equals(result)) {
				responseDto.setCode(1);
				responseDto.setMessage("success");
			}else{
				responseDto.setCode(-1);
				responseDto.setMessage(result);
			}
		}
		PrintWriter writer = response.getWriter();
		writer.write(JsonUtil.toJsonString(responseDto));
		writer.close();
		
	}

}
